<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理页面</title>
    <script src="js/vue.min.js"></script>
    <script src="js/jquery-1.12.4.js"></script>
</head>

<body>
    <div id="app">
        <h1 v-if="user!=null">欢迎光临，{{user.name}} {{user.sex=='男'?'先生':'女士'}}</h1>
        <h1 v-else>{{ tips }},5秒后回到登录页面</h1>
    </div>

    <script>
        new Vue({
            el: '#app',

            data: {
                user:null ,
                tips:''
            },

            mounted() {
                let that = this;

                // 发起请求
                $.ajax('http://localhost:9999/tiangou/api/main', {
                    type: 'GET',
                    dataType: 'json',
                    beforeSend: function (xhr) {
                        // 方法二：使用请求头的方式传递
                        xhr.setRequestHeader("Authorization", localStorage.getItem("token"))
                    },
                    success: function (res) {

                        if (res.code == 200) {
                            that.user = res.data;
                            return;
                        }

                        that.tips = res.msg;
                        setTimeout(function () {
                            location.href = "index.html"
                        }, 5000);
                    }
                });

            }
        });
    </script>
</body>

</html>